package cc.linker.steplink.crm.task;

import cc.linker.steplink.crm.merchanth5.service.EnrollActiveService;
import cc.linker.steplink.crm.merchanth5.service.acs.PurchaseProcessNewService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/7/13 15:52
 * @Description: TODO
 */
@Slf4j
@Component
@Async
public class OrderCallBack {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private PurchaseProcessNewService purchaseProcessNewService;
    @Autowired
    private EnrollActiveService enrollActiveService;


    public static final String GOODS_PAY_CALLBACK = "callbackOrder:goodsPayCallBack";
    public static final String GOODS_PAY_ERROR_CALLBACK = "callbackOrder:goodsPayErrorCallBack";

    public static final String ENROLL_PAY_CALLBACK = "callbackOrder:enrollPayCallBack";
    public static final String ENROLL_PAY_ERROR_CALLBACK = "callbackOrder:enrollPayErrorCallBack";

    /*
     * 商品支付处理1
     * */
    @Scheduled(initialDelay = 2000, fixedDelay = 500)
    public void goodsPayDispose1() {
        this.goodsPayCallback(GOODS_PAY_CALLBACK);
    }

    /*
     * 商品支付处理2
     * */
    @Scheduled(initialDelay = 2100, fixedDelay = 500)
    public void goodsPayDispose2() {
        this.goodsPayCallback(GOODS_PAY_CALLBACK);
    }

    /*
     * 商品支付失败后每隔10秒重新尝试入库
     * */
    @Scheduled(initialDelay = 10000, fixedDelay = 10000)
    public void goodsPayErrorDispose() {
        this.goodsPayCallback(GOODS_PAY_ERROR_CALLBACK);
    }

    //处理商品回调逻辑
    private void goodsPayCallback(String key) {
        Long listCount = stringRedisTemplate.opsForList().size(key);
        if (null != listCount && listCount > 0) {
            for (int i = 0; i < listCount; i++) {
                String goodsPayStr = stringRedisTemplate.opsForList().leftPop(key);
                try {
                    if (!StringUtils.isEmpty(goodsPayStr)) {
                        Map<String, Object> param = (Map<String, Object>) JSON.parse(goodsPayStr);
                        if ("20".equals(param.get("status"))) {
                            LOGGER.info("goodsPayCallback--------start");
                            purchaseProcessNewService.shopCallback(param);
                        } else {
                            //如果通联返回支付不成功
                        }
                    }
                } catch (Exception ex) {
                    LOGGER.error("goodsPayDispose-----error" + ex.getStackTrace().toString(),ex);
                    stringRedisTemplate.opsForList().rightPush(GOODS_PAY_ERROR_CALLBACK, goodsPayStr);
                }
            }
        }
    }

    /*
     * 报名支付处理1
     * */
    @Scheduled(initialDelay = 2000, fixedDelay = 500)
    public void enrollPayDispose1() {
        this.enrollPayCallBack(ENROLL_PAY_CALLBACK);
    }

    /*
     * 报名支付处理2
     * */
    @Scheduled(initialDelay = 2100, fixedDelay = 500)
    public void enrollPayDispose2() {
        this.enrollPayCallBack(ENROLL_PAY_CALLBACK);
    }

    /*
     * 商品支付失败后每隔10秒重新尝试入库
     * */
    @Scheduled(initialDelay = 10000, fixedDelay = 10000)
    public void enrollPayErrorDispose() {
        this.enrollPayCallBack(ENROLL_PAY_ERROR_CALLBACK);
    }

    //处理商品回调逻辑
    private void enrollPayCallBack(String key) {
        Long listCount = stringRedisTemplate.opsForList().size(key);
        if (null != listCount && listCount > 0) {
            for (int i = 0; i < listCount; i++) {
                String enrollPayStr = stringRedisTemplate.opsForList().leftPop(key);
                try {
                    if (!StringUtils.isEmpty(enrollPayStr)) {
                        Map<String, Object> param = (Map<String, Object>) JSON.parse(enrollPayStr);
                        if ("20".equals(param.get("status"))) {
                            if ((param.containsKey("type") && "5".equals(param.get("type").toString())) || !param.containsKey("type")) {
                                enrollActiveService.enrollCallback(param);
                            }
                        }
                    }
                } catch (Exception ex) {
                    LOGGER.error("enrollPayDispose-----error" + ex.getStackTrace().toString(),ex);
                    stringRedisTemplate.opsForList().rightPush(ENROLL_PAY_ERROR_CALLBACK, enrollPayStr);
                }
            }
        }
    }
}
